﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform;
using Ebada.UI.Base;
namespace Ebada.Modules.Vehicle
{

    public partial class UCVehicleDispatch_Right : DevExpress.XtraEditors.XtraUserControl {
        private GridViewOperation<VehicleDispatch> gridViewOperation;

        public event SendDataEventHandler<VehicleDispatch> FocusedRowChanged;
        public static bool FirstLoad = false;
        public UCVehicleDispatch_Right() 
        {
            InitializeComponent();
            gridViewOperation = new GridViewOperation<VehicleDispatch>(gridControl1, gridView1, barManager1,new frmVehicleDispatchEdit());
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridViewOperation_FocusedRowChanged);
            gridViewOperation.BeforeDelete += new ObjectOperationEventHandler<VehicleDispatch>(gridViewOperation_BeforeDelete);
            gridViewOperation.AfterDelete += new ObjectEventHandler<VehicleDispatch>(gridViewOperation_AfterDelete);
            initColumns();
        }

        void gridViewOperation_AfterDelete(VehicleDispatch obj)
        {
            if (vehnumoldlist.Count > 0)
            {
                Refresh(vehnumoldlist);
            }
            else
            {
                InitData();
            }
        }

        void gridViewOperation_BeforeDelete(object render, ObjectOperationEventArgs<VehicleDispatch> e)
        {
            if (e.Value.DispatchingTime.Year>1900)
            {
                MessageBox.Show("已派车，不能删除此条记录！");
                e.Value.VehicleDispatchID = "";
                return;
            }
        }
        
       

        void gridViewOperation_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) 
        {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as VehicleDispatch);
        }
        


       
        /// <summary>
        /// 设置隐藏列
        /// </summary>
        /// 
       public  void initColumns() 
        {
            for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                gridView1.Columns[i].VisibleIndex = -1;
            }
            int m = 0;
            if (gridView1.Columns["VehicleDispatchID"]!=null)
            {
                gridView1.Columns["VehicleDispatchID"].Caption = "派车单号";
                gridView1.Columns["VehicleDispatchID"].VisibleIndex = m++;
            }
            
            gridView1.Columns["VehicleID"].Caption = "车辆标识";
            gridView1.Columns["VehicleID"].VisibleIndex = m++;
            gridView1.Columns["VehicleNumber"].Caption = "车牌号";
            gridView1.Columns["VehicleNumber"].VisibleIndex = m++;
            gridView1.Columns["VehicleType"].Caption = "车辆类别";
            gridView1.Columns["VehicleType"].VisibleIndex = m++;
            gridView1.Columns["UsingOrgCode"].Caption = "使用单位编码";
            gridView1.Columns["UsingOrgCode"].VisibleIndex = m++;
            gridView1.Columns["UsingOrgName"].Caption = "使用单位名称";
            gridView1.Columns["UsingOrgName"].VisibleIndex = m++;
            gridView1.Columns["UsingDeptCode"].Caption = "用车部门编号";
            gridView1.Columns["UsingDeptCode"].VisibleIndex = m++;
            gridView1.Columns["UsingDeptName"].Caption = "用车部门名称";
            gridView1.Columns["UsingDeptName"].VisibleIndex = m++;
            gridView1.Columns["EmployeeNo"].Caption = "驾驶员编号";
            gridView1.Columns["EmployeeNo"].VisibleIndex = m++;
            gridView1.Columns["EmployeeName"].Caption = "驾驶员";
            gridView1.Columns["EmployeeName"].VisibleIndex = m++;
            gridView1.Columns["PhoneNum"].Caption = "联系电话";
            gridView1.Columns["PhoneNum"].VisibleIndex = m++;
            gridView1.Columns["EmployeeNum"].Caption = "随行人数";
            gridView1.Columns["EmployeeNum"].VisibleIndex = m++;
            gridView1.Columns["WaitingPlace"].Caption = "候车地点";
            gridView1.Columns["WaitingPlace"].VisibleIndex = m++;
            if (gridView1.Columns["ExpectDispatchingTime"] != null)
            {
                gridView1.Columns["ExpectDispatchingTime"].Caption = "预计派车时间";
                gridView1.Columns["ExpectDispatchingTime"].VisibleIndex = m++;
            }
           
            gridView1.Columns["ExpectReturnTime"].Caption = "预计回车时间";
            gridView1.Columns["ExpectReturnTime"].VisibleIndex = m++;
            gridView1.Columns["UsingDays"].Caption = "用车天数";
            gridView1.Columns["UsingDays"].VisibleIndex = m++;
            gridView1.Columns["Reason"].Caption = "出车原因";
            gridView1.Columns["Reason"].VisibleIndex = m++;
            gridView1.Columns["StrartKilometer"].Caption = "里程表数";
            gridView1.Columns["StrartKilometer"].VisibleIndex = m++;
            gridView1.Columns["DispatchRemark"].Caption = "调度备注";
            gridView1.Columns["DispatchRemark"].VisibleIndex = m++;
            gridView1.Columns["DispatchingTime"].Caption = "实际派车时间";
            gridView1.Columns["DispatchingTime"].VisibleIndex = m++;
            gridView1.Columns["ReturnTime"].Caption = "实际回车时间";
            gridView1.Columns["ReturnTime"].VisibleIndex = m++;
            gridView1.Columns["EndKilometer"].Caption = "返回时里程表数";
            gridView1.Columns["EndKilometer"].VisibleIndex = m++;
            gridView1.Columns["ReturnRemark"].Caption = "回车备注";
            gridView1.Columns["ReturnRemark"].VisibleIndex = m++;
            gridView1.Columns["UploadTime"].Caption = "上传时间";
            gridView1.Columns["UploadTime"].VisibleIndex = m++;
            gridView1.Columns["UploadUserID"].Caption = "上传人ID";
            gridView1.Columns["UploadUserID"].VisibleIndex = m++;
            gridView1.Columns["UploadUserID"].Visible = false;
            gridView1.Columns["UploadUserName"].Caption = "上传人";
            gridView1.Columns["UploadUserName"].VisibleIndex = m++;
            gridView1.Columns["LastEditTime"].Caption = "修改时间";
            gridView1.Columns["LastEditTime"].VisibleIndex = m++;
            gridView1.Columns["LastEditUserID"].Caption = "修改人ID";
            gridView1.Columns["LastEditUserID"].VisibleIndex = m++;
            gridView1.Columns["LastEditUserID"].Visible = false;
            gridView1.Columns["LastEditUserName"].Caption = "修改人";
            gridView1.Columns["LastEditUserName"].VisibleIndex = m++;
        }
      
    
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        [Browsable(false)]
       public GridViewOperation<VehicleDispatch> GridViewOperation
       {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(VehicleDispatch newobj)
        {
            //newobj.VehicleID = Guid.NewGuid().ToString();
        }
        private List<string> vehnumoldlist;
        public void RefreshBySql(string sql)
        {
            gridViewOperation.RefreshData(sql);
        }
        public void HideBar()
        {
            bar1.Visible = false;
            bar3.Visible = false;
        }
       
        public void Refresh(List<string> vehnumlist)
        {

            vehnumoldlist = new List<string>(vehnumlist.ToArray());
            if (vehnumlist.Count==0)
            {
                return;
            }
            string sql="";
            for (int i = 0; i < vehnumlist.Count; i++)
            {
                sql += " or  VehicleID='" + vehnumlist[i] + "' ";
            }
            if (sql.Length>3)
            {
                sql = " where  (" + sql.Substring(3, sql.Length - 3) + ") and EndKilometer=0";
            }
            gridViewOperation.RefreshData(sql);
        }
        public void InitData()
        {
            gridViewOperation.RefreshData(" where EndKilometer=0");
        }
        //回车登记

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.RowCount == 0)
            {
                return;
            }
            VehicleDispatch tempvhi = (VehicleDispatch)gridView1.GetFocusedRow();

            frmVehicleDispatch_BakEdit frm = new frmVehicleDispatch_BakEdit();
            tempvhi.ReturnTime = DateTime.Now;
            frm.RowData = tempvhi;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Vehicles tempvehic = MainHelper.PlatformSqlMap.GetOneByKey<Vehicles>(tempvhi.VehicleID);
                tempvehic.VehicleState = "待命";
                if (tempvehic.VehicleImage == null)
                {
                    tempvehic.VehicleImage = new byte[0];
                }
                MainHelper.PlatformSqlMap.Update<Vehicles>(tempvehic);
                MainHelper.PlatformSqlMap.Update<VehicleDispatch>((VehicleDispatch)frm.RowData);
                UCVehicleDispatch.ParentForm.ucVehicleDispatch_Left1.ISRefresh = true;
                UCVehicleDispatch.ParentForm.ucVehicleDispatch_Left1.InitData();
                if (vehnumoldlist.Count > 0)
                {
                    Refresh(vehnumoldlist);
                }
                else
                {
                    InitData();
                }
                MessageBox.Show("回车登记已完成！");

            }
            
        }
        InVehicleDispatch outvehic = new CVehicleDispatch();
       //出车标记
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
             if (gridView1.RowCount == 0)
            {
                return;
            }
            VehicleDispatch tempvhi = (VehicleDispatch)gridView1.GetFocusedRow();
            if (outvehic.OutVehiclesSign(tempvhi.VehicleDispatchID))
            {
                UCVehicleDispatch.ParentForm.ucVehicleDispatch_Left1.InitData();
                if (vehnumoldlist.Count > 0)
                {
                    Refresh(vehnumoldlist);
                }
                else
                {
                    InitData();
                }
                MessageBox.Show("出车标记已完成！");
            }
           
        }
       
       
      

    }
}
